{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f514e2d9-b31a-42bb-94c8-4476e4abb305",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install burr[start]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:12:50.561804Z",
     "start_time": "2024-03-27T00:12:50.531039Z"
    },
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "import random\n",
    "import time\n",
    "from typing import Tuple\n",
    "\n",
    "import cowsay\n",
    "\n",
    "from burr.core import Action, Application, ApplicationBuilder, State, default, expr\n",
    "from burr.core.action import action\n",
    "from burr.lifecycle import PostRunStepHook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f5b9bfc78851a59",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:13:03.529875Z",
     "start_time": "2024-03-27T00:13:03.524449Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# create some hooks!\n",
    "class PrintWhatTheCowSaid(PostRunStepHook):\n",
    "    def post_run_step(self, *, state: \"State\", action: \"Action\", **future_kwargs):\n",
    "        if action.name != \"cow_should_say\" and state[\"cow_said\"] is not None:\n",
    "            print(state[\"cow_said\"])\n",
    "\n",
    "\n",
    "class CowCantSpeakFast(PostRunStepHook):\n",
    "    def __init__(self, sleep_time: float):\n",
    "        super(PostRunStepHook, self).__init__()\n",
    "        self.sleep_time = sleep_time\n",
    "\n",
    "    def post_run_step(self, *, state: \"State\", action: \"Action\", **future_kwargs):\n",
    "        if action.name != \"cow_should_say\":  # no need to print if we're not saying anything\n",
    "            time.sleep(self.sleep_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5b29e9de2ca536b9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:13:12.756137Z",
     "start_time": "2024-03-27T00:13:12.750764Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# instantiate actions\n",
    "@action(reads=[], writes=[\"cow_said\"])\n",
    "def cow_said(state: State, say_what: list[str]) -> Tuple[dict, State]:\n",
    "    said = random.choice(say_what)\n",
    "    result = {\"cow_said\": cowsay.get_output_string(\"cow\", said) if say_what is not None else None}\n",
    "    return result, state.update(**result)\n",
    "\n",
    "\n",
    "@action(reads=[], writes=[\"cow_should_speak\"])\n",
    "def cow_should_speak(state: State) -> Tuple[dict, State]:\n",
    "    result = {\"cow_should_speak\": random.randint(0, 3) == 0}\n",
    "    return result, state.update(**result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "271b868317fbbdf4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:14:10.836543Z",
     "start_time": "2024-03-27T00:14:10.829258Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": [
    "# build application\n",
    "hooks = [\n",
    "    PrintWhatTheCowSaid(),\n",
    "    CowCantSpeakFast(sleep_time=2.0),\n",
    "]\n",
    "        \n",
    "app = (ApplicationBuilder()\n",
    "    .with_state(cow_said=None)\n",
    "    .with_actions(\n",
    "        say_nothing=cow_said.bind(say_what=None),\n",
    "        say_hello=cow_said.bind(\n",
    "            say_what=[\"Hello world!\", \"What's up?\", \"Are you Aaron Burr, sir?\"]\n",
    "        ),\n",
    "        cow_should_speak=cow_should_speak,\n",
    "    )\n",
    "    .with_transitions(\n",
    "        (\"cow_should_speak\", \"say_hello\", expr(\"cow_should_speak\")),\n",
    "        (\"say_hello\", \"cow_should_speak\", default),\n",
    "        (\"cow_should_speak\", \"say_nothing\", expr(\"not cow_should_speak\")),\n",
    "        (\"say_nothing\", \"cow_should_speak\", default),\n",
    "    )\n",
    "    .with_entrypoint(\"cow_should_speak\")\n",
    "    .with_hooks(*hooks)\n",
    "    .build()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "794bc0abda9e48ad",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:14:29.829383Z",
     "start_time": "2024-03-27T00:14:29.511355Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 9.0.0 (20230911.1827)\n",
       " -->\n",
       "<!-- Pages: 1 -->\n",
       "<svg width=\"192pt\" height=\"209pt\"\n",
       " viewBox=\"0.00 0.00 192.02 209.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 205)\">\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-205 188.02,-205 188.02,4 -4,4\"/>\n",
       "<!-- say_nothing -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>say_nothing</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M89.27,-201C89.27,-201 31.27,-201 31.27,-201 25.27,-201 19.27,-195 19.27,-189 19.27,-189 19.27,-177 19.27,-177 19.27,-171 25.27,-165 31.27,-165 31.27,-165 89.27,-165 89.27,-165 95.27,-165 101.27,-171 101.27,-177 101.27,-177 101.27,-189 101.27,-189 101.27,-195 95.27,-201 89.27,-201\"/>\n",
       "<text text-anchor=\"middle\" x=\"60.27\" y=\"-177.95\" font-family=\"Times,serif\" font-size=\"14.00\">say_nothing</text>\n",
       "</g>\n",
       "<!-- cow_should_speak -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>cow_should_speak</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M107.65,-118.5C107.65,-118.5 12.9,-118.5 12.9,-118.5 6.9,-118.5 0.9,-112.5 0.9,-106.5 0.9,-106.5 0.9,-94.5 0.9,-94.5 0.9,-88.5 6.9,-82.5 12.9,-82.5 12.9,-82.5 107.65,-82.5 107.65,-82.5 113.65,-82.5 119.65,-88.5 119.65,-94.5 119.65,-94.5 119.65,-106.5 119.65,-106.5 119.65,-112.5 113.65,-118.5 107.65,-118.5\"/>\n",
       "<text text-anchor=\"middle\" x=\"60.27\" y=\"-95.45\" font-family=\"Times,serif\" font-size=\"14.00\">cow_should_speak</text>\n",
       "</g>\n",
       "<!-- say_nothing&#45;&gt;cow_should_speak -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>say_nothing&#45;&gt;cow_should_speak</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M58.99,-164.74C58.7,-159.97 58.42,-154.78 58.27,-150 58.04,-142.67 58.04,-140.83 58.27,-133.5 58.31,-132.3 58.35,-131.08 58.41,-129.85\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"61.88,-130.45 58.91,-120.27 54.89,-130.07 61.88,-130.45\"/>\n",
       "</g>\n",
       "<!-- say_hello -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>say_hello</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M82.15,-36C82.15,-36 38.4,-36 38.4,-36 32.4,-36 26.4,-30 26.4,-24 26.4,-24 26.4,-12 26.4,-12 26.4,-6 32.4,0 38.4,0 38.4,0 82.15,0 82.15,0 88.15,0 94.15,-6 94.15,-12 94.15,-12 94.15,-24 94.15,-24 94.15,-30 88.15,-36 82.15,-36\"/>\n",
       "<text text-anchor=\"middle\" x=\"60.27\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">say_hello</text>\n",
       "</g>\n",
       "<!-- say_hello&#45;&gt;cow_should_speak -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>say_hello&#45;&gt;cow_should_speak</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M93.05,-36.42C97.97,-40.53 102.37,-45.38 105.27,-51 108.63,-57.52 108.63,-60.98 105.27,-67.5 104.09,-69.78 102.67,-71.94 101.06,-73.97\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"98.65,-71.43 94.11,-81 103.63,-76.35 98.65,-71.43\"/>\n",
       "</g>\n",
       "<!-- cow_should_speak&#45;&gt;say_nothing -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>cow_should_speak&#45;&gt;say_nothing</title>\n",
       "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M60.27,-118.93C60.27,-128.95 60.27,-141.78 60.27,-153.25\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"56.77,-153.02 60.27,-163.02 63.77,-153.02 56.77,-153.02\"/>\n",
       "<text text-anchor=\"middle\" x=\"122.15\" y=\"-136.7\" font-family=\"Times,serif\" font-size=\"14.00\">not cow_should_speak</text>\n",
       "</g>\n",
       "<!-- cow_should_speak&#45;&gt;say_hello -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>cow_should_speak&#45;&gt;say_hello</title>\n",
       "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M16.41,-82.17C10.87,-78.17 5.95,-73.33 2.52,-67.5 -4.42,-55.72 3.91,-45.49 16.2,-37.49\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"17.9,-40.55 24.87,-32.57 14.44,-34.46 17.9,-40.55\"/>\n",
       "<text text-anchor=\"middle\" x=\"54.65\" y=\"-54.2\" font-family=\"Times,serif\" font-size=\"14.00\">cow_should_speak</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x1107a98a0>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "app.visualize(include_conditions=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2facfb753233cd7d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:16:40.234406Z",
     "start_time": "2024-03-27T00:16:32.211734Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ____________\n",
      "| Hello world! |\n",
      "  ============\n",
      "            \\\n",
      "             \\\n",
      "               ^__^\n",
      "               (oo)\\_______\n",
      "               (__)\\       )\\/\\\n",
      "                   ||----w |\n",
      "                   ||     ||\n",
      "  ____________\n",
      "| Hello world! |\n",
      "  ============\n",
      "            \\\n",
      "             \\\n",
      "               ^__^\n",
      "               (oo)\\_______\n",
      "               (__)\\       )\\/\\\n",
      "                   ||----w |\n",
      "                   ||     ||\n",
      "  ____________\n",
      "| Hello world! |\n",
      "  ============\n",
      "            \\\n",
      "             \\\n",
      "               ^__^\n",
      "               (oo)\\_______\n",
      "               (__)\\       )\\/\\\n",
      "                   ||----w |\n",
      "                   ||     ||\n",
      "  ____________\n",
      "| Hello world! |\n",
      "  ============\n",
      "            \\\n",
      "             \\\n",
      "               ^__^\n",
      "               (oo)\\_______\n",
      "               (__)\\       )\\/\\\n",
      "                   ||----w |\n",
      "                   ||     ||\n"
     ]
    }
   ],
   "source": [
    "# run things!\n",
    "for i in range(0, 4):\n",
    "    # step through 4 times\n",
    "    a_obj, result, state = app.step()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fed65721e7cc457a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-27T00:17:18.640978Z",
     "start_time": "2024-03-27T00:17:10.619878Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  ________________________\n",
      "| Are you Aaron Burr, sir? |\n",
      "  ========================\n",
      "                        \\\n",
      "                         \\\n",
      "                           ^__^\n",
      "                           (oo)\\_______\n",
      "                           (__)\\       )\\/\\\n",
      "                               ||----w |\n",
      "                               ||     ||\n",
      "  ________________________\n",
      "| Are you Aaron Burr, sir? |\n",
      "  ========================\n",
      "                        \\\n",
      "                         \\\n",
      "                           ^__^\n",
      "                           (oo)\\_______\n",
      "                           (__)\\       )\\/\\\n",
      "                               ||----w |\n",
      "                               ||     ||\n",
      "  ____________\n",
      "| Hello world! |\n",
      "  ============\n",
      "            \\\n",
      "             \\\n",
      "               ^__^\n",
      "               (oo)\\_______\n",
      "               (__)\\       )\\/\\\n",
      "                   ||----w |\n",
      "                   ||     ||\n",
      "  ____________\n",
      "| Hello world! |\n",
      "  ============\n",
      "            \\\n",
      "             \\\n",
      "               ^__^\n",
      "               (oo)\\_______\n",
      "               (__)\\       )\\/\\\n",
      "                   ||----w |\n",
      "                   ||     ||\n"
     ]
    }
   ],
   "source": [
    "# Run some more\n",
    "for i in range(0, 4):\n",
    "    # step through 4 times\n",
    "    a_obj, result, state = app.step()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19095ae96e98fd83",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
